package log

import (
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

type GatewayLog struct {
	*zap.Logger
	logfile string
	zapConf zap.Config
}

func New(file string, level string) (*GatewayLog, error) {
	l := &GatewayLog{
		logfile: file,
	}

	cfg := zap.NewProductionConfig()
	cfg.OutputPaths = []string{file}
	cfg.DisableStacktrace = true
	cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder

	zapLevel := zap.InfoLevel
	switch level {
	case "debug":
		zapLevel = zap.DebugLevel
	case "info":
		zapLevel = zap.InfoLevel
	case "warn":
		zapLevel = zap.WarnLevel
	case "error":
		zapLevel = zap.ErrorLevel
	case "panic":
		zapLevel = zap.PanicLevel
	default:
	}

	cfg.Level = zap.NewAtomicLevelAt(zapLevel)

	zapLog, err := cfg.Build()
	if err != nil {
		return nil, err
	}

	l.zapConf = cfg
	l.Logger = zapLog.Named("gateway")

	return l, nil
}
